<?php
/**
 * view browse class
 */
//require_once(PATH_FRAME.'smartyFunc.php');
require_once SMARTY_DIR.'Smarty.class.php';

class View extends Smarty
{
    public
        $templateFile = "",
        $templateFileType = "",
        $templateGroup = null,
        $isEdit = false,
        $isList = false,
        $isConfirm = false,
        $clearTempalteFileAndCacheIdArray = array(),
        $cacheID = null,
        $param = array(),
        $errors = array()
    ;

    public $parent;
    private $result;
    
    function __construct()
    {
        parent::__construct();
        $this->template_dir = SMARTY_TEMP; //temp
        $this->compile_dir  = SMARTY_COMP; //compile
        $this->config_dir   = SMARTY_CONF; //config
        $this->cache_dir    = SMARTY_CACH; //cache
        
        $this->left_delimiter = '{{'     ; //デリミタ変更
        $this->right_delimiter = '}}'    ; //{*} -> {{*}}
        
        $this->cache_lifetime = -1;
        
        $this->param = array(
            'name' => SITE_NAME,
            'url'  => URL_SITE,
            'edit' => URL_EDIT,
            'page' => substr(HOST_SITE, 0, -1) . preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']),
            'css'  => URL_CSS,
            'js'   => URL_JS,
            'date' => SITE_CSS_VERSION,
            'image'=> URL_IMAGE,
            
            'local'=> LOCAL_MODE,
            'cached'=> false,
            
            'nameHeader' => null,
        );
    }

    function addResult($returning, $data)
    {
        if ($returning) {
            $this->result[$returning] = $data;
        }
        else {
            $this->result['solo'] = $data;
        }
        return $data;
    }

    function publishHTML()
    {
        global $myGlobal;
        
        if ($this->cacheID) $this->caching = 1;
        
        //query
        $this->param['query'] = isset($myGlobal['query']) ? $myGlobal['query'] : array(); 
        
        //pageName
        if ($loadName = $this->getTemplateVars('loadName')) {
            $val = current($loadName);
            $val = @$this->result[key($loadName)]->$val;
            if ($val) $this->assign("loadName",$val);
        };
        
        //pankuzu
        if ($pankuzu = $this->getTemplateVars('pankuzu')) {
            $ret = array();
            foreach ($pankuzu as $v) {
                if (isset($v['load'])) {
                    $val = current($v['load']);
                    $v['load'] = $this->result[key($v['load'])]->$val;
                }
                $ret[] = $v;
            }
            $this->assign("pankuzu", $ret);
        }
        
        //config
        $this->configLoad('name.txt');
        
        if ($this->isConfirm) {
            $this->param['nameHeader'] = '[確認]';
        }
        else if ($this->isEdit) {
            $this->param['nameHeader'] = '[編集]';
        }
        
        $this->assign("data",$this->result);
        $this->assign("_param",$this->param);
        if (!empty($_POST)) $this->assign("_errors",$this->errors);

        echo $this->fetch( $this->getTemplateFile(), $this->cacheID );
        
        $this->caching = 0;
    }

    function isCachedPage()
    {
        $this->caching = 1;
        
        //キャッシュの有無を確認
        $ret = $this->isCached( $this->getTemplateFile(), $this->cacheID );
        
        $this->param['cached'] = $ret;
        
        $this->caching = 0;
        
        return $ret;
    }


    function clearAddedCacheFiles ()
    {
        foreach ($this->clearTempalteFileAndCacheIdArray as $v) {
            list ($templateFile, $cacheID, $compiledID, $time) = $v;
            if ($templateFile) $templateFile .= TMP_EXT;
            $this->clearCache($templateFile, $cacheID);
        }
    }

    private function getTemplateFile ()
    {
        if ($this->templateFile) {
            return $this->templateFile . TMP_EXT;
        }
        $templateFile = ($this->isEdit) ? SMARTY_EDIT_DIR : SMARTY_PAGE_DIR;
        $templateFile .= ($this->templateGroup ?  $this->templateGroup : $this->templateFileType) . '/' . $this->templateFileType;
        $templateFile .= ($this->isList)
            ? ($this->isEdit ? '.edit' : '.list')
            : ($this->isEdit 
                ? ( $this->isConfirm ? '.confirm' : '.form') 
                : '.solo'
            );
        $templateFile .= TMP_EXT;
        return $templateFile;
    }
    
}